package com.bxd.baodan.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.Tag;
import com.bxd.baodan.activity.MainActivity;
import com.bxd.baodan.item.UserItem;
import com.bxd.baodan.util.DesUtil;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.NetUtil.RequestStringListener;
import com.bxd.baodan.util.SaveMsgUtil;
import com.bxd.baodan.util.SpUtil;
import com.bxd.baodan.util.StringUtil;

/**
 * 登录信息
 * 
 * @author oldfeel Create on: 2014年7月8日
 */
public class LoginInfo {
	private static LoginInfo loginInfo;
	private static UserItem userItem;



	public static LoginInfo getInstance(Context context) {
		if (loginInfo == null) {
			loginInfo = new LoginInfo(context);
		}
		return loginInfo;
	}
	public static LoginInfo getInstance() {
		if (loginInfo == null) {
			loginInfo = new LoginInfo();
		}
		return loginInfo;
	}


	public boolean isFirst() {
		boolean isFirst = SpUtil.getInstance().getBoolean(
				SharedPreferanceKey.IS_FIRST_START);
		if (isFirst) {
			SpUtil.getInstance().savaBoolean(
					SharedPreferanceKey.IS_FIRST_START, false);
		}
		return isFirst;
	}
	public boolean isWelFirst() {
		boolean isFirst = SpUtil.getInstance().getBoolean(
				SharedPreferanceKey.IS_WEL_FIRST_START);
		if (isFirst) {
			SpUtil.getInstance().savaBoolean(
					SharedPreferanceKey.IS_WEL_FIRST_START, false);
		}
		return isFirst;
	}

	public boolean isgetFirst() {
		boolean isFirst = SpUtil.getInstance().getBoolean("isgetFirst");
		if (isFirst) {
			SpUtil.getInstance().savaBoolean("isgetFirst", false);
			return true;
		}
		return false;
	}

	public LoginInfo(Context context) {

		userItem = new Gson().fromJson(
				DesUtil.decode(SpUtil.getInstance().getString("logininfo")),
				UserItem.class);
		if (userItem == null) {
			userItem = new UserItem();
		}
	}

	public LoginInfo() {

		userItem = new Gson().fromJson(
				DesUtil.decode(SpUtil.getInstance().getString("logininfo")),
				UserItem.class);
		if (userItem == null) {
			userItem = new UserItem();
		}
	}

	public void saveUserInfo(String string, Context activity) {
		SpUtil.getInstance().savaString("logininfo", DesUtil.encode(string));
		// editor.commit();
		userItem = new Gson().fromJson(string, UserItem.class);
		// if(!string.equals("")){
		SaveMsgUtil.updateLocalInfo(userItem, activity);// 保存登陆后的用户信息到本地

		// }
	}

	/**
	 * 保存登录信息
	 * 
	 * @param account
	 *            登录帐号
	 * @param password
	 *            登录密码
	 */
	public void saveLoginInfo(String account, String password) {
		SpUtil.getInstance().savaString("account", account);
		SpUtil.getInstance().savaString("password", password);
	}

	public boolean isLogin() {
		return userItem != null && userItem.getU_id() != null;
	}

	/**
	 * 注销登录
	 */
	public void cancelLogin(Context context) {
		saveUserInfo("", context);
		boolean isRelogin = SpUtil.getInstance().getBoolean(
				SharedPreferanceKey.IS_RELOGIN);
		String account = SpUtil.getInstance().getString(SharedPreferanceKey.ACCOUNT);
		SpUtil.getInstance().clearCache();
		SpUtil.getInstance().savaString(SharedPreferanceKey.ACCOUNT, account);
		SpUtil.getInstance().savaBoolean(SharedPreferanceKey.IS_RELOGIN, isRelogin);
		SpUtil.getInstance().savaBoolean(SharedPreferanceKey.IS_FIRST_START,false);

		SharedPreferences settings = context.getSharedPreferences(
				Constant.APP_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.remove(SharedPreferanceKey.TEMPICURL);
		editor.commit();
		
		
		context.sendBroadcast(new Intent(Constant.ACTION_LOGOUT));
		Intent intent = new Intent(context, MainActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
		context.startActivity(intent);
	}

	public UserItem getUser() {
		if (userItem == null) {
			return new UserItem();
		}
		return userItem;
	}

	public String getUid() {
		if (userItem!= null && userItem.getU_id()!= null) {
			return userItem.getU_id();
		}
		return "";
	}

	public boolean isAutoLogin() {
		if (userItem != null && !StringUtil.isEmpty(userItem.getU_id())) {
			return true;
		}
		return false;
	}

	public String getAccount() {
		return SpUtil.getInstance().getString("account");
	}

	public String getRealPassword() {
		return SpUtil.getInstance().getString("password");
	}
	
	public void close() {
		loginInfo = null;
	}

	/**
	 * 刷新登录资料
	 * 
	 * @param activity
	 */
	public static void refresh(final Activity activity) {
		final LoginInfo info = LoginInfo.getInstance(activity);
		NetUtil netUtil = new NetUtil(activity, JsonApi.LOGIN);
		netUtil.setParams("telphone", info.getAccount());
		netUtil.setParams("password", info.getRealPassword());
		netUtil.setParams("vcode", SpUtil.getInstance().getInteger("etVcode"));
		netUtil.setParams("type", SpUtil.getInstance().getInteger("isVcode"));
		// TODO: 2017/3/23 添加字段
		netUtil.postRequest("", new RequestStringListener() {

			@Override
			public void onComplete(String result) {
				if (JSONUtil.isSuccess(result)) {
					info.saveUserInfo(JSONUtil.getData(result), activity);
					SpUtil.getInstance().savaString(
							SharedPreferanceKey.ACCOUNT, info.getAccount());
					UserItem user = info.getUser();
					SaveMsgUtil.updateLocalInfo(user, activity);// 保存登陆后的用户信息到本地
					updatePushTags(activity.getApplicationContext());
					activity.sendBroadcast(new Intent(
							Constant.ACTION_SUCCESS_LOGIN));// 发送广播更新界面
					activity.finish();
				}
			}
		});
	}
	
	/**
	 * 更新个推标签状态
	 * */
	public static void updatePushTags(Context context){
		//刷新信鸽推送标签
		String tag = "";
       
		String city = userItem.getCity();
		if(city != null && !"".equals(city)&&!"null".equals(city)){
			tag+=city+",";
		}					
		String tags[]=tag.split(",");
		  Tag[] tagParam = new Tag[tags.length];
        for (int i = 0; i < tags.length; i++) {
            Tag t = new Tag();
            t.setName(tags[i]);
            tagParam[i] = t;
        }
		PushManager.getInstance().setTag(context, tagParam,"");
	}
}
